<head>
  <style>
    body { margin: 0; }

    #time {
      position: absolute;
      bottom: 8px;
      left: 8px;
      color: lightblue;
      font-family: monospace;
    }
  </style>

  <script type="importmap">{ "imports": {
    "react": "https://esm.sh/react",
    "react-dom": "https://esm.sh/react-dom/client"
  }}</script>

<!--  <script type="module">import * as React from 'react'; window.React = React;</script>-->
<!--  <script src="../../dist/react-globe.gl.js" defer></script>-->
</head>

<body>
<div id="globeViz"></div>

<script src="//unpkg.com/@babel/standalone"></script>
<script type="text/jsx" data-type="module">
  import Globe from 'https://esm.sh/react-globe.gl?external=react';
  import React, { useState, useEffect } from 'react';
  import { createRoot } from 'react-dom';
  import { MeshLambertMaterial } from 'https://esm.sh/three';
  import * as solar from 'https://esm.sh/solar-calculator';

  const VELOCITY = 9; // minutes per frame

  const solarMaterial = new MeshLambertMaterial({ color: '#ffff00', opacity: 0.3, transparent: true });

  const sunPosAt = dt => {
    const day = new Date(+dt).setUTCHours(0, 0, 0, 0);
    const t = solar.century(dt);
    const longitude = (day - dt) / 864e5 * 360 - 180;
    return [longitude - solar.equationOfTime(t) / 4, solar.declination(t)];
  };

  const World = () => {
    const [dt, setDt] = useState(+new Date());

    useEffect(() => {
      (function iterateTime() {
        setDt(dt => dt + VELOCITY * 60 * 1000);
        requestAnimationFrame(iterateTime);
      })();
    }, []);

    return <div>
      <Globe
        globeImageUrl="//cdn.jsdelivr.net/npm/three-globe/example/img/earth-dark.jpg"
        tilesData={[{ pos: sunPosAt(dt) }]}
        tileLng={d => d.pos[0]}
        tileLat={d => d.pos[1]}
        tileAltitude={0.005}
        tileWidth={180}
        tileHeight={180}
        tileUseGlobeProjection={false}
        tileMaterial={() => solarMaterial}
        tilesTransitionDuration={0}
      />
      <div id="time">{new Date(dt).toLocaleString()}</div>
    </div>;
  };

  createRoot(document.getElementById('globeViz'))
    .render(<World />);
</script>
</body>